<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/Entite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/database/Database.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/exceptions/DAOException.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/role/RoleEntite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/string/StringUtils.php';

class RoleDAO {
	
	private function RoleDAO() {
		//évite la construction
	}
		
	public static function getListeRole() {
		$database = Database::getInstance();
		$result = $database->executeQueryAndClose("SELECT id, code, libelle FROM role ORDER BY libelle");
		$roleArray = Array();
		foreach ($result as $row) {
			$roleArray[] = RoleDAO::getRolesFromDatabaseRow($row);
		}
		return $roleArray;
	}
	
	
	
	public static function getListeRoleById($listeIdRole) {
		if (count($listeIdRole) == 0) {
			return Array();
		}
		$database = Database::getInstance();
		$listeIdAsString = StringUtils::getArrayAsString($listeIdRole);
		$result = $database->executeQueryAndClose("SELECT id, code, libelle FROM role WHERE id IN ($listeIdAsString) ORDER BY libelle");
		$roleArray = Array();
		foreach ($result as $row) {
			$roleArray[] = RoleDAO::getRolesFromDatabaseRow($row);
		}
		return $roleArray;
	}
	
	public static function getRoleByID($id) {
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, code, libelle FROM role WHERE id =$id");
		foreach ($result as $row){
			return RoleDAO::getRolesFromDatabaseRow($row);
		}
		throw new MissingDataException("Impossible de trouver le rôle avec l'ID ${id}");
	}
	
	public static function save(RoleEntite $roleEntite) {
		if($roleEntite->id == Entite::getUnpersistedId()) {
			$result = RoleDAO::insert($roleEntite);
		} else {
			$result = RoleDAO::update($roleEntite);
			if ($result > 1) {
				$libelle = $roleEntite->libelle;
				throw new DAOException("La sauvegarde du rôle '$libelle' a mis à jour $result lignes");
			}
		}
	}
	
	public static function delete($id) {
		$result = Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM role WHERE id = $id") ;
		return $result;
	}
	
	private static function insert(RoleEntite $roleEntite) {
		$libelle = mysql_escape_string($roleEntite->libelle);
		$code = mysql_escape_string($roleEntite->code);
		
		$result = Database::getInstance()->executeInsertQuery("INSERT INTO role (code, libelle) values ('$code', '$libelle')") ;
		return $result;
	}
	
	private static function update(RoleEntite $roleEntite) {
		$libelle = mysql_escape_string($roleEntite->libelle);
		$code = mysql_escape_string($roleEntite->code);
		$result = Database::getInstance()->executeUpdateQueryAndClose("UPDATE role SET code='$code', libelle = '$libelle' WHERE id = $roleEntite->id");
		return $result;
	}
	
	private static function getRolesFromDatabaseRow($row) {
		$roleEntite = new RoleEntite();
		$roleEntite->id = $row['id'];
		$roleEntite->code = $row['code'];
		$roleEntite->libelle = $row['libelle'];
		return $roleEntite;
	}
}
?>